package com.videoanswer.van.persistence.dao.user.impl;

import com.videoanswer.van.common.dao.hibernate.HibernateDao;
import com.videoanswer.van.persistence.dao.user.UserDaoService;
import com.videoanswer.van.persistence.domain.user.User;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Darren
 * Date: 10/16/13
 * Time: 1:53 PM
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class UserDaoServiceImpl extends HibernateDao<User> implements UserDaoService{
    public UserDaoServiceImpl() {
        super(User.class);
    }

    @Override
    public User insertUser(User user) {

        return save(user);
    }

    @Override
    public User login(String email, String password) {

        if(!StringUtils.hasText(email) || !StringUtils.hasText(password)){
            return null;
        }

        String hql = "select u from User u where u.email = :email and u.password = :password";
        String[] param = new String[]{"email","password"};
        String[] value = new String[]{email, password};

        List<User> rs = executeHQL(hql,param,value);

        return rs != null ? !rs.isEmpty() ? rs.get(0) : null : null;
    }
}
